<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>registerPlugin()</title>
<meta name="generator" content="DocBook XSL Stylesheets V1.75.2">
<link rel="home" href="index.html" title="Smarty 3 Manual">
<link rel="up" href="api.functions.html" title="Chapter 14. Smarty Class Methods">
<link rel="prev" href="api.register.filter.html" title="registerFilter()">
<link rel="next" href="api.register.object.html" title="registerObject()">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<div class="navheader">
<table width="100%" summary="Navigation header">
<tr><th colspan="3" align="center">registerPlugin()</th></tr>
<tr>
<td width="20%" align="left">
<a accesskey="p" href="api.register.filter.html">Prev</a> </td>
<th width="60%" align="center">Chapter 14. Smarty Class Methods</th>
<td width="20%" align="right"> <a accesskey="n" href="api.register.object.html">Next</a>
</td>
</tr>
</table>
<hr>
</div>
<div class="refentry" title="registerPlugin()">
<a name="api.register.plugin"></a><div class="titlepage"></div>
<div class="refnamediv">
<h2>Name</h2>
<p>registerPlugin() — dynamically register plugins</p>
</div>
<div class="refsect1" title="Description">
<a name="id438034"></a><h2>Description</h2>
<code class="methodsynopsis"><span class="type">void </span><span class="methodname">registerPlugin</span>(<span class="methodparam"><span class="type">string </span><span class="parameter">type</span></span>,<br>                    <span class="methodparam"><span class="type">string </span><span class="parameter">name</span></span>,<br>                    <span class="methodparam"><span class="type">mixed </span><span class="parameter">callback</span></span>,<br>                    <span class="methodparam"><span class="type">bool </span><span class="parameter">cacheable</span></span>,<br>                    <span class="methodparam"><span class="type">mixed </span><span class="parameter">cache_attrs</span></span>);</code><p>
   This method registers functions or methods defined in your script as plugin.
   It uses the following parameters:
   </p>
<div class="itemizedlist"><ul class="itemizedlist" type="disc">
<li class="listitem"><p>
<em class="parameter"><code>type</code></em> defines the type of the plugin. Valid values are "function", "block", "compiler" and "modifier".
</p></li>
<li class="listitem"><p>
<em class="parameter"><code>name</code></em> defines the name of the plugin.
</p></li>
<li class="listitem">
<p>
<em class="parameter"><code>callback</code></em> defines the PHP callback. it can be either:
   </p>
<div class="itemizedlist"><ul class="itemizedlist" type="circle">
<li class="listitem"><p>
       A string containing the function <em class="parameter"><code>name</code></em>
       </p></li>
<li class="listitem"><p>
       An array of the form <code class="literal">array(&amp;$object, $method)</code> with
       <code class="literal">&amp;$object</code> being a reference to an
       object and <code class="literal">$method</code> being a string
       containing the method-name
       </p></li>
<li class="listitem"><p>
       An array of the form
       <code class="literal">array($class, $method)</code> with
       <code class="literal">$class</code> being the class name and
       <code class="literal">$method</code> being a method of the class.
       </p></li>
</ul></div>
<p>
   </p>
</li>
<li class="listitem"><p>
     <em class="parameter"><code>cacheable</code></em> and <em class="parameter"><code>cache_attrs</code></em> can be
     omitted in most cases. See <a class="link" href="caching.cacheable.html" title="Controlling Cacheability of Output">controlling cacheability of plugins output</a>
     on how to use them properly.
    </p></li>
</ul></div>
<p>
  </p>
<div class="example">
<a name="id438564"></a><p class="title"><b>Example 14.39. register a function plugin</b></p>
<div class="example-contents">
<pre class="programlisting">

&lt;?php
$smarty-&gt;registerPlugin("function","date_now", "print_current_date");

function print_current_date($params, $smarty)
{
  if(empty($params["format"])) {
    $format = "%b %e, %Y";
  } else {
    $format = $params["format"];
  }
  return strftime($format,time());
}
?&gt;

   </pre>
<p>
   And in the template
   </p>
<pre class="programlisting">

{date_now}

{* or to format differently *}
{date_now format="%Y/%m/%d"}

</pre>
</div>
</div>
<br class="example-break"><div class="example">
<a name="id438594"></a><p class="title"><b>Example 14.40. register block function plugin</b></p>
<div class="example-contents">
<pre class="programlisting">

&lt;?php
// function declaration
function do_translation ($params, $content, $smarty, &amp;$repeat, $template)
{
  if (isset($content)) {
    $lang = $params["lang"];
    // do some translation with $content
    return $translation;
  }
}

// register with smarty
$smarty-&gt;registerPlugin("block","translate", "do_translation");
?&gt;

   </pre>
<p>
    Where the template is:
   </p>
<pre class="programlisting">

{translate lang="br"}Hello, world!{/translate}

   </pre>
</div>
</div>
<br class="example-break"><div class="example">
<a name="id438624"></a><p class="title"><b>Example 14.41. register modifier plugin</b></p>
<div class="example-contents">
<pre class="programlisting">

&lt;?php

// let's map PHP's stripslashes function to a Smarty modifier.
$smarty-&gt;registerPlugin("modifier","ss", "stripslashes");

?&gt;

</pre>
<p>In the template, use <code class="literal">ss</code> to strip slashes.</p>
<pre class="programlisting">

&lt;?php
{$var|ss}
?&gt;

</pre>
</div>
</div>
<br class="example-break"><p>
See also
<a class="link" href="api.unregister.plugin.html" title="unregisterPlugin"><code class="varname">unregisterPlugin()</code></a>,
<a class="link" href="plugins.functions.html" title="Template Functions">plugin functions</a>,
<a class="link" href="plugins.block.functions.html" title="Block Functions">plugin block functions</a>,
<a class="link" href="plugins.compiler.functions.html" title="Compiler Functions">plugin compiler functions</a>,
 and the
 <a class="link" href="plugins.modifiers.html" title="Modifiers">creating plugin modifiers</a> section.
</p>
</div>
</div>
<div class="navfooter">
<hr>
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left">
<a accesskey="p" href="api.register.filter.html">Prev</a> </td>
<td width="20%" align="center"><a accesskey="u" href="api.functions.html">Up</a></td>
<td width="40%" align="right"> <a accesskey="n" href="api.register.object.html">Next</a>
</td>
</tr>
<tr>
<td width="40%" align="left" valign="top">registerFilter() </td>
<td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td>
<td width="40%" align="right" valign="top"> registerObject()</td>
</tr>
</table>
</div>
</body>
</html>
